<template>
  <el-dialog title="信息编辑"
             :close-on-click-modal="false"
             :visible.sync="visible">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="姓名"
                    prop="name">
        <el-input v-model="dataForm.name"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input v-model="dataForm.idCard" placeholder="请输入身份证号"></el-input>
      </el-form-item>
      <el-form-item label="年龄" prop="idCageard">
        <el-input v-model="dataForm.age" placeholder="请输入年龄"></el-input>
      </el-form-item>
      <el-form-item label="民族" prop="nation">
        <el-input v-model="dataForm.nation" placeholder="请输入民族"></el-input>
      </el-form-item>
      <el-form-item label="户籍所在地" prop="domicile">
        <el-input v-model="dataForm.domicile" placeholder="请输入户籍所在地"></el-input>
      </el-form-item>
      <el-form-item label="派遣" prop="dispatchCompany">
        <el-input v-model="dataForm.dispatchCompany" placeholder="请输入派遣"></el-input>
      </el-form-item>
      <el-form-item label="入职企业" prop="entryCompany">
        <el-input v-model="dataForm.entryCompany" placeholder="请输入入职企业"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary"
                 @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { isEmail, isMobile } from '@/utils/validate'
  export default {
    data () {
      var validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        roleList: [],
        dataForm: {
          id: null,
          name: null,
          mobile: '',
          idCard: '',
          age: '',
          nation: '',
          domicile: '',
          dispatchCompany: '',
          entryCompany: ''
        },
        dataRule: {
          mobile: [{
            required: true, message: '手机号不能为空', trigger: 'blur'
          }, {
            validator: validateMobile, trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.getUserInfo(this.dataForm.id)
          }
        })
      },

      getUserInfo (id) {
        this.$http({
          url: this.$http.adornUrl('/user/info'),
          method: 'get',
          params: this.$http.adornParams({id: id})
        }).then(({ data }) => {
          console.log(data)
          console.log(111)
          this.dataForm = data
        })
      },

      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl('/user/update'),
              method: 'post',
              data: this.$http.adornData(this.dataForm)
            }).then(({ data }) => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        })
      }
    }
  }
</script>
